#quit on error
#set -e

res_url=https://gitee.com/calfenyin/public/raw/master/envsetup/centos

alias curl='curl -H "cache-control:no-cache"'

setup_color() {
  # Only use colors if connected to a terminal
  if [ -t 1 ]; then
    RED=$(printf '\033[31m')
    GREEN=$(printf '\033[32m')
    LIGHT_GREEN=$(printf '\033[1;32m')
    YELLOW=$(printf '\033[33m')
    BLUE=$(printf '\033[34m')
    BOLD=$(printf '\033[1m')
    RESET=$(printf '\033[m')
  else
    RED=""
    GREEN=""
    YELLOW=""
    BLUE=""
    BOLD=""
    RESET=""
  fi
}

setup_color

command_exists() {
  command -v "$@" >/dev/null 2>&1
}

is_local_user() {
  grep "^`whoami`:" /etc/passwd >/dev/null 2>&1
}

add_vim_rc() {
  echo $1 | sudo tee -a /etc/vimrc > /dev/null
}

add_bin() {
  [[ ! -d /util ]] && mkdir /util
  chmod 755 /util
  [[ ! -f /util/$1 ]] && curl -fsSL $res_url/res/$1 -o /util/$1
  chmod 755 /util/$1
}

# usefule tools
echo -n "checking exa installation... "
add_bin exa
echo ${LIGHT_GREEN}done${RESET}

echo -n "checking peco installation... "
add_bin peco
echo ${LIGHT_GREEN}done${RESET}
#add_bin ack

echo -n "checking thefuck installation... "
if (! command_exists fuck) then
  sudo yum install -y python3-devel
  python -m pip install thefuck 2>/dev/null || python3 -m pip install thefuck
fi
echo ${LIGHT_GREEN}done${RESET}

echo -n "checking zsh installation... "
if (! command_exists zsh) then
  sudo yum install -y zsh
fi
echo ${LIGHT_GREEN}done${RESET}

echo -n "checking git installation... "
if (! command_exists git) then
  sudo yum install -y git
fi
echo ${LIGHT_GREEN}done${RESET}

echo -n "checking jq installation... "
if (! command_exists jq) then
  echo "jq not installed, trying to install"
  sudo yum install -y jq
fi
echo ${LIGHT_GREEN}done${RESET}

echo -n "checking icdiff installation... "
if (! command_exists icdiff) then
  python -m pip install icdiff 2>/dev/null || python3 -m pip install icdiff
fi
echo ${LIGHT_GREEN}done${RESET}

echo -n "checking git conf... "
# setup git alias and icdiff
git_user_name=`git config --global user.name`
git_user_email=`git config --global user.email`

curl -fsSL $res_url/res/gitconfig -o ~/.gitconfig

# keep old username and email
git config --global user.name $git_user_name > /dev/null
git config --global user.email $git_user_email > /dev/null
echo ${LIGHT_GREEN}done${RESET}

echo -n "checking chsh installation... "
if (! command_exists chsh) then
  sudo yum install -y util-linux-user
fi
echo ${LIGHT_GREEN}done${RESET}

echo -n "checking ohmyzsh installation... "
if [ ! -d ~/.oh-my-zsh ]; then
  #sh -c "$(curl -fsSLk https://raw.kgithub.com/ohmyzsh/ohmyzsh/master/tools/install.sh)";
  sh -c "$(curl -fsSLk $res_url/res/ohmyz.sh)";
fi
echo ${LIGHT_GREEN}done${RESET}

echo -n "checking zsh-autosuggestions installation... "
if [ ! -d ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions ]; then
  git clone https://kgithub.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
fi
echo ${LIGHT_GREEN}done${RESET}

echo -n "checking zsh-syntax-highlighting installation... "
if [ ! -d ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting ]; then
  git clone https://kgithub.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
fi
echo ${LIGHT_GREEN}done${RESET}

echo -n "downloading alias and utility... "
sudo curl -fsSL $res_url/res/alias.sh -o /etc/profile.d/alias.sh > /dev/null 2>&1
sudo chmod 644 /etc/profile.d/alias.sh
echo ${LIGHT_GREEN}done${RESET}

echo -n "downloading zsh theme... "
curl -fsSL $res_url/res/cyin.zsh-theme -o ~/.oh-my-zsh/themes/cyin.zsh-theme > /dev/null

date=$(date '+%Y%m%d_%H%M%S')
mv ~/.zshrc ~/.zshrc.$date
echo ${LIGHT_GREEN}done${RESET}

echo -n "downloading .zshrc... "
curl -fsSL $res_url/res/zshrc.sh -o ~/.zshrc > /dev/null
echo ${LIGHT_GREEN}done${RESET}

#username=`whoami
userhome=`echo ~`

#escape / char
#userhome=$(echo "${userhome}" | sed -e 's/[]$.*[\^]/\\&/g' )
userhome=$(echo "${userhome}" | sed -e 's/\//\\&/g' )

sed -i "s/\[USER_HOME\]/${userhome}/g" ~/.zshrc > /dev/null

#chsh -s /bin/zsh

if (! is_local_user) then
    if (! sudo grep "default_shell" /etc/sssd/sssd.conf) then  #skip if already added
        #change default shell for domain users
        sudo sed -ie "/\[domain/a\default_shell = /bin/zsh" /etc/sssd/sssd.conf
        sudo rm -rf /var/lib/sss/db/*
        sudo systemctl restart sssd
    fi
fi

# change zsh highlighters color
sed -i "s/,underline//g" ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/highlighters/main/main-highlighter.zsh > /dev/null
sed -i "s/green/cyan/g" ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/highlighters/main/main-highlighter.zsh > /dev/null
sed -i "s/=underline/=/g" ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/highlighters/main/main-highlighter.zsh > /dev/null
sed -i "s/{ZSH_HIGHLIGHT_STYLES\[arg0\]:=fg=green}/{ZSH_HIGHLIGHT_STYLES[arg0]:=fg=green,bold}/g" ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/highlighters/main/main-highlighter.zsh > /dev/null
sed -i "s/{ZSH_HIGHLIGHT_STYLES\[autodirectory\]:=fg=green,underline}/{ZSH_HIGHLIGHT_STYLES[autodirectory]:=fg=yellow}/g" ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/highlighters/main/main-highlighter.zsh > /dev/null

# VIM settings

echo -n "adding vim settings... "
sep='""""""""""""""VIM_Settings_CYIN""""""""""""""'

# delete existing settings between sep
sudo sed -i "/$sep/,/$sep/d" /etc/vimrc

# append settings
add_vim_rc "$sep"
add_vim_rc 'color desert'
add_vim_rc 'set nu'
add_vim_rc 'filetype on'
add_vim_rc 'set cursorline'
add_vim_rc 'hi CursorLine term=none cterm=bold ctermbg=gray'
add_vim_rc 'nmap <C-L> :set hlsearch!<CR>'
add_vim_rc "$sep"
#add_vim_rc 'xxxxxxxxxxxxxxxxx'
echo ${LIGHT_GREEN}done${RESET}

exec zsh -l
